package com.example.template.dao;

import com.example.template.bean.User;
import com.example.template.dao.mapper.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by admin on 2018/4/11.
 */
@Repository
public class UserDao extends BaseDao {
    private  static final Logger logger= LoggerFactory.getLogger(UserDao.class);

    @Autowired
    private UserMapper userMapper;

    public void create(String name, Integer age) {
        jdbcTemplate.update("insert into USER(NAME, AGE) values(?, ?)", name, age);
    }


    public List<User> selectAllUsers() {
        String sql="SELECT * FROM `user`";
        logger.debug(sql);
        List<User> users=jdbcTemplate.query(sql,userMapper);
        return users;
    }

    public User selectUserById(int id) {
        String sql="SELECT * FROM `user` WHERE `id` = ? ";
        logger.debug(sql+"[parameter:"+id+"]");
        List<User> users=jdbcTemplate.query(sql,userMapper,id);
        return users==null?null:users.get(0);
    }
}
